<template>
  <div class='container'>
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      label-position="left"
      label-width="100px"
      style="width: 80%; margin-left: 10px"
    >
      <el-form-item
        label="文章标题"
        prop="title"
      >
        <el-input
          v-model="form.title"
          class="input"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="请输入内容"
        prop="articleBody"
        ref="formDateContent"
      >
        <quill-editor
          v-model="form.articleBody"
          :options="editorOption"
          @blur="hBlurContent"
        ></quill-editor>
      </el-form-item>

      <el-form-item label="视频链接">
        <el-input
          v-model="form.videoURL"
          class="input"
        ></el-input>
      </el-form-item>

      <el-form-item style="text-align: center;">
        <el-button
          type="primary"
          @click="hSubmit"
        >确定</el-button>
        <el-button @click="hClose">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  props: {
    data: {
      type: Object,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  components: {
    quillEditor
  },
  data () {
    return {
      editorOption: {
        placeholder: '',
        modules: {
          // 自定义工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      },
      form: {
        title: '', // 文章标题
        articleBody: '', // 文章内容
        videoURL: null // 视频链接
      },
      rules: {
        title: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      }
    }
  },
  created () {
    if (this.isEdit === true) {
      this.form = this.data
    }
  },
  methods: {
    // 添加
    async doAdd () {
      try {
        const res = await add(this.form)
        console.log(res)
        this.$message.success('添加成功')
        this.$emit('add')
      } catch (err) {
        this.$message.error('添加失败')
      }
    },
    // 编辑
    async doEdit () {
      try {
        const res = await update(this.form)
        console.log(res)
        this.$message.success('编辑成功')
        this.$emit('add')
      } catch (err) {
        this.$message.error('编辑失败')
      }
    },
    // 富文本失去焦点
    hBlurContent () {
      this.$refs.formDateContent.validate('blur')
    },
    // 确定
    hSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          // this.doAdd()
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    hClose () {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>
</style>
